<?php
/**[留言表模型]
 * @Author: 976123967@qq.com
 * @Date:   2014-09-16 11:28:51
 * @Last Modified by:   Administrator
 * @Last Modified time: 2014-11-11 20:27:52
 */
class FeedbackModel  extends Model{
	protected $tableName='feedback';

	/**
	 * [$_validate 自动验证]
	 * @var array
	 */
	public  $_validate = array(
		
		array('people','require','Please filled in people',1,'regex',1),
		array('phone','require','Please filled in phone',1,'regex',1),
		array('email','require','Please filled in email',1,'regex',1),
		array('email','email','Email error',1,'regex',1),
		array('body','require','Please filled in content',1,'regex',1),
	);

	/**
	 * [$_auto 自动完成]
	 * @var array
	 */
	public $_auto = array(
		// 留言时间
		array('addtime','time',1,'function'),
		array('sn','_sn',1,'callback'),
	);
	protected function _sn()
	{
		return Date('YmdHis');
	}
	
	/**
	 * [add_feedback 添加]
	 */
	public function add_feedback()
	{
		$this->add();
	}


	/**
	 * [get_feedback_all 所有留言]
	 * @return [type] [description]
	 */
	public function get_feedback_all()
	{

		$count = $this->count();
		import('ORG.Util.Page');// 导入分页类
		$page = new Page($count,20);
		$data = $this->limit($page->firstRow.','.$page->listRows)->order('lookstate asc,addtime desc')->select();
		if(!$data) return false;
			
		foreach($data as $k=>$v)
		{
			$data[$k]['addtime']= date("Y-m-d H:i:s",$v['addtime']);
		}
		$data['page'] = $page->show();

		return $data;
	}



	/**
	 * [get_feedabck_one 单条留言记录]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public function get_feedabck_one($id)
	{
		return $this->find($id);
	}

	/**
	 * [update_state 更新状态]
	 * @param  [type] $fid [description]
	 * @return [type]      [description]
	 */
	public function update_state($fid,$state)
	{
		$this->where(array('fd_id'=>$fid))->save(array('lookstate'=>$state));

	}


	/**
	 * [del_feedback 删除留言]
	 * @param  [type] $fid [description]
	 * @return [type]      [description]
	 */
	public function del_feedback($fid)
	{
		$this->where(array('fd_id'=>$fid))->delete();
	}

	/**
	 * [del_feedback_all 删除选择的留言]
	 * @return [type] [description]
	 */
	public function del_feedback_all()
	{
		$fdId = I('post.fd_id');

		/**
		 * 验证
		 */
		foreach($fdId as $v)
		{
			if(!is_numeric($v))
			{
				$this->error='提交信息有误';
				return false;
			}

			$data= $this->get_feedabck_one($v);
			if(!$data)
			{
				$this->error='提交信息有误';
				return false;
			}
		}

		// 删除
		foreach($fdId as $v)
		{
			$this->del_feedback($v);
		}
		return true;
	}


	// 验证码
	public function check_code()
	{
		$code = I('post.code',null,'strtoupper');

		if($code != session('code'))
		{
			$this->error=array('cn'=>'验证码错误','en'=>'verify code is error');
			return false;
		}
		return true;
	}

	/**
	 * [search 查询]
	 * @return [type] [description]
	 */
	public function search()
	{

		$sn = I('post.sn');
		if(!$sn) return false;
		$where['sn']=$sn;
		
		return $this->where($where)->find();

	}
}